package com.zrwl.poscloud.xo.service.impl;
import com.zrwl.poscloud.base.entity.TeacherAdminR;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.service.AdminService;
import com.zrwl.poscloud.xo.vo.AdminVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import com.zrwl.poscloud.xo.mapper.TeacherAdminRMapper;
import com.zrwl.poscloud.xo.service.TeacherAdminRService;
import com.zrwl.poscloud.xo.vo.TeacherAdminRVO;

import javax.annotation.Resource;

/**
 * 管理员教师关联 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-04-13
 */
@Service
public class TeacherAdminRServiceImpl extends SuperServiceImpl<TeacherAdminRMapper,TeacherAdminR> implements TeacherAdminRService {

    @Autowired
    private TeacherAdminRService teacherAdminRService;
    @Resource
    private TeacherAdminRMapper teacherAdminRMapper;
    @Autowired
    private AdminService adminService;
    @Override
    public IPage<TeacherAdminR> getPageList(TeacherAdminRVO teacherAdminRVO){
       QueryWrapper<TeacherAdminR> queryWrapper = new QueryWrapper<>();
       if (StringUtils.isNotEmpty(teacherAdminRVO.getKeyword()) && !StringUtils.isEmpty(teacherAdminRVO.getKeyword().trim())) {
            queryWrapper.like("name", teacherAdminRVO.getKeyword().trim());
       }
       Page<TeacherAdminR> page = new Page<>();
       page.setCurrent(teacherAdminRVO.getCurrentPage());
       page.setSize(teacherAdminRVO.getPageSize());
       queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
       queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
       IPage<TeacherAdminR> pageList = teacherAdminRService.page(page, queryWrapper);
       List<TeacherAdminR> list = pageList.getRecords();
       pageList.setRecords(list);
       return pageList;
}
    @Override
    public List<TeacherAdminR> getAllList(TeacherAdminRVO teacherAdminRVO){
        QueryWrapper<TeacherAdminR> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<TeacherAdminR> list = teacherAdminRService.list(queryWrapper);
        return list;
}
    @Override
    public String add(TeacherAdminRVO teacherAdminRVO){
        TeacherAdminR entity = new TeacherAdminR();
        BeanUtils.copyProperties(teacherAdminRVO, entity, SysConf.STATUS);
        entity.insert();
        teacherAdminRVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);}
    @Override
    public String edit(TeacherAdminRVO teacherAdminRVO){
        TeacherAdminR entity = teacherAdminRService.getById(teacherAdminRVO.getUid());
        BeanUtils.copyProperties(teacherAdminRVO, entity);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}
    @Override
    public String delete(TeacherAdminRVO teacherAdminRVO){
        TeacherAdminR entity = teacherAdminRService.getById(teacherAdminRVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}

    @Override
    public String editAdmin(TeacherAdminRVO teacherAdminRVO) {
        if(StringUtils.isNotBlank(teacherAdminRVO.getUid())){
            TeacherAdminR entity = teacherAdminRService.getById(teacherAdminRVO.getUid());
            entity.setEmployeeUid(teacherAdminRVO.getEmployeeUid());
            entity.updateById();
        }else {
           add(teacherAdminRVO);
        }
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    /**
     * 根据adminUid获取关联教师信息
     *
     * @param adminUid 管理员uid
     * @return 教师管理员
     */
    @Override
    public TeacherAdminRVO getTeacherByAdminUid(String adminUid) {
        return teacherAdminRMapper.getTeacherBYAdminUid(adminUid);
    }

    /**
     * 根据employeeUid获取关联教师信息
     *
     * @param employeeUid 持卡人uid
     * @return 教师管理员
     */
    @Override
    public List<TeacherAdminRVO> getAdminByTeacherUid(String employeeUid) {
        return teacherAdminRMapper.getAdminByTeacherUid(employeeUid);
    }

    /**
     * 获取当前教师信息
     *
     * @return 教师信息
     */
    @Override
    public TeacherAdminRVO getCurrentTeacher() {
        AdminVO adminVO = adminService.getMe();
        if(adminVO == null){
            throw new RuntimeException("当前管理员不可为空");
        }
        return teacherAdminRMapper.getTeacherBYAdminUid(adminVO.getUid());
    }
}
